﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmAreaContrato : Form
    {
        [Dependency]
        public IAreaContratoBL areaContratoBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmAreaContrato()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        public USUARIO currentUsuarioAreaContrato { get; set; }

        private void FrmAreaContrato_Load(object sender, EventArgs e)
        {
            if (currentUsuarioAreaContrato != null)
            {
                menuLlena();
            }
            else
                cargaData();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioAreaContrato.TRABAJADOR.PERSONA.Dni);

            var permiso = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.IdUsuario)
                           select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[]{
            this.btnEliminar,this.btnEditar,this.btnNuevo};

            var noVisible = from nv in botones
                            where !permiso.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            cargaData();
        }

        public void cargaData()
        {
            aREA_CONTRATOBindingSource.DataSource = areaContratoBL.GetAllFromAreaContrato();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            aREA_CONTRATOBindingSource.DataSource = areaContratoBL.GetAllFromAreaContratoByDescripcion(txtDescripcion.Text);
        }

        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) 
            {
                aREA_CONTRATOBindingSource.DataSource = areaContratoBL.GetAllFromAreaContratoByDescripcion(txtDescripcion.Text);
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoAreaContrato frm = new FrmMttoAreaContrato();
            frm.NuevoAreaContrato();
            frm.ShowDialog();
            cargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var areaContrato = aREA_CONTRATOBindingSource.Current as AREA_CONTRATO;

            if (areaContrato != null)
            {
                FrmMttoAreaContrato frm = new FrmMttoAreaContrato();
                frm._areaContrato = areaContrato;
                frm.EditarAreaContrato();
                frm.ShowDialog();
                cargaData();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Area.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var areaContrato = aREA_CONTRATOBindingSource.Current as AREA_CONTRATO;

            if (areaContrato != null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea Eliminar el Area: " + areaContrato.Descripcion + "?"),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                if (rpt == DialogResult.Yes)
                {
                    areaContratoBL.DeleteAreaContrato(areaContrato.IdAreaContrato);
                    cargaData();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Area.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        public AREA_CONTRATO currentAreaContrato { get; set; }

        private void aREA_CONTRATORadGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                currentAreaContrato = aREA_CONTRATOBindingSource.Current as AREA_CONTRATO;
                this.Close();
            }
        }

    }
}
